package ljl.codetop;

import commons.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class _103_zigzag_level_order {
    
    static class tryout {
        public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
            List<List<Integer>> res = new ArrayList<>();
            LinkedList<TreeNode> q = new LinkedList<>();
            q.offer(root);
            while (!q.isEmpty()) {
                int l = q.size();
                LinkedList<Integer> level = new LinkedList<>();
                res.add(level);
                for (int i = 0; i < l; i++) {
                    TreeNode node = q.poll();
                    if (res.size() % 2 == 1)
                        level.offer(node.val);
                    else level.offerFirst(node.val);
                    if (node.left != null) q.offer(node.left);
                    if (node.right != null) q.offer(node.right);
                }
            }
            return res;
        }
    }
}
